import React from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Shield, Zap, RefreshCw } from "lucide-react";

// 导入共享组件
import StringArrayEditor from "./SharedComponents/StringArrayEditor.jsx";
import GroundingWireEditor from "./SharedComponents/GroundingWireEditor";

const EmergencyPlan = ({
  localTemplate,
  handleFieldChange,
  isLiveCrossingMethod,
  isGeneratingEmergency,
  emergencyGenResult,
  onGenerateEmergency
}) => {
  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <h3 className="text-lg font-semibold flex items-center gap-2">
          <Shield className="w-5 h-5 text-orange-600" />
          应急预案模板
        </h3>
        <div className="flex items-center gap-3">
          {emergencyGenResult && (
            <span className="text-sm text-gray-600 bg-gray-100 px-2 py-1 rounded">
              {emergencyGenResult}
            </span>
          )}
          {/* <Button
            type="button"
            onClick={onGenerateEmergency}
            disabled={isGeneratingEmergency}
            variant="outline"
            className="bg-orange-50 border-orange-200 text-orange-700 hover:bg-orange-100"
          >
            {isGeneratingEmergency ? (
              <>
                <RefreshCw className="w-4 h-4 mr-2 animate-spin" />
                生成中...
              </>
            ) : (
              <>
                <Shield className="w-4 h-4 mr-2" />
                AI生成应急预案
              </>
            )}
          </Button> */}
        </div>
      </div>

      {isLiveCrossingMethod && (
        <Card className="p-4 bg-orange-50/50 border-orange-200">
          <CardHeader className="p-0 pb-4">
            <CardTitle className="text-lg flex items-center gap-2 text-orange-800">
              <Zap className="w-5 h-5" />
              带电跨越施工安全措施模板
            </CardTitle>
          </CardHeader>
          <CardContent className="p-0 space-y-4">
            <div>
              <Label className="block text-sm font-medium mb-1">
                停电范围模板
              </Label>
              <Input
                value={localTemplate.powerOutageScopeTemplate || ""}
                onChange={(e) =>
                  handleFieldChange(
                    "powerOutageScopeTemplate",
                    e.target.value
                  )
                }
                placeholder="例如：110kV某某线#1-#5塔"
              />
            </div>

            <GroundingWireEditor
              items={localTemplate.groundingWirePositionsTemplate || []}
              onUpdate={(value) =>
                handleFieldChange("groundingWirePositionsTemplate", value)
              }
            />

            <StringArrayEditor
              title="带电跨越专项安全措施模板"
              items={localTemplate.liveCrossingSafetyMeasuresTemplate || []}
              onUpdate={(value) =>
                handleFieldChange("liveCrossingSafetyMeasuresTemplate", value)
              }
              placeholder="例如：确认线路已停电并悬挂接地线"
            />

            <StringArrayEditor
              title="需协调部门模板"
              items={localTemplate.coordinationDepartmentsTemplate || []}
              onUpdate={(value) =>
                handleFieldChange("coordinationDepartmentsTemplate", value)
              }
              placeholder="例如：调度中心"
            />

            <div>
              <Label className="block text-sm font-medium mb-1">
                应急联系信息模板
              </Label>
              <div className="grid grid-cols-2 gap-2 p-3 bg-white rounded-lg border">
                <Input
                  value={localTemplate.emergencyContactInfoTemplate?.dispatcherPhone || ""}
                  onChange={(e) =>
                    handleFieldChange("emergencyContactInfoTemplate", {
                      ...(localTemplate.emergencyContactInfoTemplate || {}),
                      dispatcherPhone: e.target.value,
                    })
                  }
                  placeholder="调度中心电话"
                />
                <Input
                  value={localTemplate.emergencyContactInfoTemplate?.emergencyRepairPhone || ""}
                  onChange={(e) =>
                    handleFieldChange("emergencyContactInfoTemplate", {
                      ...(localTemplate.emergencyContactInfoTemplate || {}),
                      emergencyRepairPhone: e.target.value,
                    })
                  }
                  placeholder="抢修电话"
                />
                <Input
                  value={localTemplate.emergencyContactInfoTemplate?.projectManagerPhone || ""}
                  onChange={(e) =>
                    handleFieldChange("emergencyContactInfoTemplate", {
                      ...(localTemplate.emergencyContactInfoTemplate || {}),
                      projectManagerPhone: e.target.value,
                    })
                  }
                  placeholder="项目经理电话"
                />
                <Input
                  value={localTemplate.emergencyContactInfoTemplate?.safetySupervisorPhone || ""}
                  onChange={(e) =>
                    handleFieldChange("emergencyContactInfoTemplate", {
                      ...(localTemplate.emergencyContactInfoTemplate || {}),
                      safetySupervisorPhone: e.target.value,
                    })
                  }
                  placeholder="安全主管电话"
                />
              </div>
            </div>
          </CardContent>
        </Card>
      )}

      <StringArrayEditor
        title="通用应急处置措施"
        items={localTemplate.commonEmergencyMeasures || []}
        onUpdate={(value) =>
          handleFieldChange("commonEmergencyMeasures", value)
        }
        placeholder="例如: '高处坠落事故处置措施'"
      />
    </div>
  );
};

export default EmergencyPlan;
